package airthmetic.exercise.tree;

public class _50_powxn {
    public double myPow(double x, int n) {
        // 分治思想： x^n = x^(n/2) * x^ (n/2)
        if(n<0){
            return 1/recur(x,-n);
        }else{
            return recur(x,n);
        }

    }

    public double recur(double x, int n){
        if(n==0){
            return 1.0;
        }
        if(n == 1){
            return x;
        }

        double res = recur(x,n/2);
        if(n % 2 == 0){
            return res * res;
        }else{
            return res * res * x;
        }


    }
}
